// JavaScript Document
function OnSubmitForm(url) { 
   document.appForm.action = url;
   document.appForm.submit();
   return true;
}

function checkCheckBox(){
	var theForm = document.appForm;
	var countCheckBox = 0;
	for (i = 0; i < theForm.elements.length; i++) {
		if (theForm.elements[i].name == 'cid[]')
		{
	        if (theForm.elements[i].checked == true){
	        	countCheckBox++;
	        }
	    }
	}
	return countCheckBox;
}

function gotoPage(id_tab, action, type, page) {
	$.ajax({
		'url' 	: '/ajax/' + action,
		'type' 	: 'post',
		'data' 	: {'page' : page, 'type' : type, 'idTab' : id_tab},
		'success':function(response) {
			var data = $.parseJSON(response);
        	$(id_tab).html(data.content);
		}
	});
}

function changeReadmore(obj, id_readmore, page) {
	var id = $(obj).attr('id');
	$(id_readmore).attr('href', '/' + page + '/index/loaitin/' + id);
}

var confirmed = false;
function confirmDialog(obj, url) {
    if(!confirmed) {
        $( "#question" ).dialog({
            resizable: false,
            closeOnEscape: true,
            width: 'auto',
            minHeight: 120,
            maxHeight: 200,
            modal: true,
            buttons: [
	            {
	            	text: "Yes",
	            	"class": 'btn btn-primary',
	            	click: function()
	                {
	                    $( this ).dialog( "close" );
	                    confirmed = true;
	                    window.location = url;
	                }
	            },
	            {
	            	text: "No",
	            	"class": 'btn',
	                click: function()
	                {
	                    $( this ).dialog( "destroy" );
	                }
	            }
            ],
            title: "Thông báo"
        });
    }
 
    return confirmed;
}

function confirmDialogDelMulti(obj, url) {
    if(!confirmed) {
        $( "#question" ).dialog({
            resizable: false,
            closeOnEscape: true,
            width: 'auto',
            minHeight: 120,
            maxHeight: 200,
            modal: true,
            buttons: [
	            {
	            	text: "Yes",
	            	"class": 'btn btn-primary',
	            	click: function()
	                {
	                    $( this ).dialog( "close" );
	                    var countCheckBox = checkCheckBox();
	                	
	                	if (countCheckBox == 0) {
	                		window.alert('Không có dòng nào được chọn.');
	                		confirmed = false;
	                	} else {
		                    document.appForm.action = url;
		                    document.appForm.submit();
		                    confirmed = true;
	                	}
	                }
	            },
	            {
	            	text: "No",
	            	"class": 'btn',
	                click: function()
	                {
	                    $( this ).dialog( "destroy" );
	                    confirmed = false;
	                }
	            }
            ],
            title: "Thông báo"
        });
    }
 
    return confirmed;
}

function getErrorHtml(formErrors, id) {
    var o = '<ul id="errors-'+ id +'" class="errors">';
    if(formErrors){
	    if (formErrors.length == undefined) {
		    for(errorKey in formErrors) {
		   		o += '<li>' + formErrors[errorKey] + '</li>';
		    }
	    } else {
	    	o += '<li>' + formErrors + '</li>';
	    }
	    o += '</ul>';
	    return o;
    }
}

function validate(evt) {
	var theEvent = evt || window.event;
  	if( theEvent.which != 8 && theEvent.which != 0 && (theEvent.which < 48 || theEvent.which > 57) ) {
    	theEvent.returnValue = false;
    	if(theEvent.preventDefault) theEvent.preventDefault();
  	}
}

function getDistrict(option, thanh_pho, quan_huyen) {
			
	var id = $(thanh_pho).val();
	var className = "dd2 " + $('#idquanhuyen_msdd').attr('class');
	var url = '/ajax/district/';
	url += id;
	$.ajax({
		'url':url,
		'type':'post',
		'data':{'id':id},
		'success':function(response) {
			var data = $.parseJSON(response);
			$(quan_huyen).empty();
		    var i = 0;
		    var l = data.length;
		    var options = '';
		    for (i = 0; i < l; i++) {
		   		options += "<option value=";
		   		options += data[i].id + ">";
		   		options += data[i].ten;
		   		options += "</option>";
		    }
		    
		    $(quan_huyen).html(options);
		    if (option == true) {
		    	$('#idquanhuyen').msDropDown({mainCSS:className}).data('dd');
		    }
		}
	});
}